var timer = null; 
function showAppointment(id)
{
	var AJAX = getAJAX();
	var id = id;
	var url = 'index.php?pagename=Appointment&id='+id
		var param = '';
	AJAX.open('POST', url, true);
	AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	AJAX.setRequestHeader("Content-length", param.length);
	AJAX.setRequestHeader("Connection", "close");

	AJAX.onreadystatechange = function()
	{
		if(AJAX.readyState == 4)
		{
			var result = AJAX.responseText;
			showModal(result);			
		}	
	}
	AJAX.send(param);	
}

function deleteAppointment()
{
	var AJAX = getAJAX();
	var id = document.getElementById("deleteAppointmentId").value;
	var param = 'id='+id;
	var occurance = document.getElementById('deleteOccurance').checked;
	if(occurance)
	{
		param = param + '&occurance=all';
	}
	var submit = document.getElementById('deleteSubmit').value;
	param = param + '&deleteSubmit=' + submit;
	var url = 'index.php?pagename=Appointment&action=delete';

	AJAX.open('POST', url, true);
	AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	AJAX.setRequestHeader("Content-length", param.length);
	AJAX.setRequestHeader("Connection", "close");

	AJAX.onreadystatechange = function()
	{
		if(AJAX.readyState == 4)
		{
			var result = AJAX.responseText;
			sendToModal(result);	
			var modal = document.getElementById('modal').onclick=function(){closeModalWithRefresh()};	
		}	
	}
	AJAX.send(param);	

}

function updateAppointment()
{
	var AJAX = getAJAX();
	var id = document.getElementById("updateAppointmentId").value;
	var param = '';
	var url = 'index.php?pagename=Appointment&action=update';
	
	if (document.getElementById("updateAppointmentId").value)
	{
		var id = document.getElementById("updateAppointmentId").value;
		param += 'id='+id; 
	}
	if (document.getElementById("startHour").value)
	{
		var startHour = document.getElementById("startHour").value;
		param += '&startHour=' + startHour; 
	}
	if (document.getElementById("startMinute").value)
	{
		var startMinute = document.getElementById("startMinute").value;
		param += '&startMinute=' + startMinute; 
	}
	if (document.getElementById("startPartDay"))
	{
		if (document.getElementById("startPartDay").value)
		{
			var startPartDay = document.getElementById("startPartDay").value;
			param += '&startPartDay=' + startPartDay; 
		}	
	}
	if (document.getElementById("endHour").value)
	{
		var endHour = document.getElementById("endHour").value;
		param += '&endHour=' + endHour; 
	}
	if (document.getElementById("endMinute").value)
	{
		var endMinute = document.getElementById("endMinute").value;
		param += '&endMinute=' + endMinute; 
	}
	if (document.getElementById("endPartDay"))
	{
		if (document.getElementById("endPartDay").value)
		{
			var endPartDay = document.getElementById("endPartDay").value;
			param += '&endPartDay=' + endPartDay; 
		}	
	}
	if (document.getElementById("updateDescriptionAppointment").value)
	{
		var description = document.getElementById("updateDescriptionAppointment").value;
		param += '&description=' + description; 
	}
	if (document.getElementById("employee").value)
	{
		var employee = document.getElementById("employee").value;
		param += '&employee=' + employee; 
	}
	if (document.getElementById("updateOccurance").checked)
	{
		var occurance = document.getElementById("updateOccurance").value;
		param += '&occurance=' + occurance; 
	}
	

	AJAX.open('POST', url, true);
	AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	AJAX.setRequestHeader("Content-length", param.length);
	AJAX.setRequestHeader("Connection", "close");

	AJAX.onreadystatechange = function()
	{
		if(AJAX.readyState == 4)
		{
			var result = AJAX.responseText;
			sendToModal(result);
			var modal = document.getElementById('modal').onclick=function(){closeModalWithRefresh()};		
		}	
	}
	AJAX.send(param);	

}

function sendToModal(result)
{
	var result = result;
	document.getElementById ("window").innerHTML = result;
}

function showModal (result)
{
	var result = result;
	var height = (window.innerHeight? window.innerHeight: 
			(document.documentElement.clientHeight? document.documentElement.clientHeight: 
			 document.body.offsetHeight));
	var width = (window.innerWidth? window.innerWidth: 
			(document.documentElement.clientWidth? document.documentElement.clientWidth: 
			 document.body.offsetWidth));
	var top = Math.ceil(height / 2 - 110);
	var left = Math.ceil(width / 2 - 210);
	opacity = 20;
	document.getElementById ("modal").style.height = height+"px";
	document.getElementById ("window").style.top = top+"px";
	document.getElementById ("window").style.left = left+"px";
	document.getElementById ("window").style.display = 'block';
	document.getElementById ("window").innerHTML = result;

	timer = setInterval ("shadow()", 1);
}

function shadow ()
{
	document.getElementById ("modal").style.display = 'block';	
	document.getElementById ("modal").style.opacity = opacity++	/ 100
	document.getElementById ("modal").style.MozOpacity = opacity	/ 100;
	document.getElementById ("modal").style.filter = "Alpha(Opacity="+opacity+")";
	if (opacity >= 80)
	{
		clearInterval (timer);
	}	
}

function closeModal()
{
	clearInterval (timer);
	document.getElementById ("modal").style.display = 'none';
	document.getElementById ("window").style.display = 'none';
}
function closeModalWithRefresh()
{
	clearInterval (timer);
	document.getElementById ("modal").style.display = 'none';
	document.getElementById ("window").style.display = 'none';
	location.reload(true);
}